package com.github.ecbp.store.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.ecbp.common.data.service.impl.BaseServiceImpl;
import com.github.ecbp.common.data.utils.PagingUtil;
import com.github.ecbp.common.data.utils.TransformUtils;
import com.github.ecbp.store.service.dto.ProductAttributeDTO;
import com.github.ecbp.store.service.dto.ProductAttributeQueryDTO;
import com.github.ecbp.store.service.entity.ProductAttribute;
import com.github.ecbp.store.service.entity.ProductAttributeCategoryRelation;
import com.github.ecbp.store.service.mapper.ProductAttributeCategoryRelationMapper;
import com.github.ecbp.store.service.mapper.ProductAttributeMapper;
import com.github.ecbp.store.service.mapper.ProductCategoryMapper;
import com.github.ecbp.store.service.service.ProductAttributeService;
import com.github.ecbp.store.service.vo.ProductAttributeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author Created by Alex·He on 2020/4/29 11:31
 * @Version 1.0
 * @Description 商品属性表(ProductAttribute)表服务实现类
 */
@Service
public class ProductAttributeServiceImpl extends BaseServiceImpl<ProductAttributeMapper, ProductAttribute>
        implements ProductAttributeService {

    @Autowired
    private ProductAttributeCategoryRelationMapper relationMapper;
    @Autowired
    private ProductCategoryMapper categoryMapper;

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/4/29 14:12
     * @version 1.0
     * @description 保存商品属性，并建立商品属性与商品分类的关系
     */
    @Override
    public boolean save(ProductAttributeDTO param) {
        ProductAttribute attribute = new ProductAttribute();
        attribute.setAttribute(param.getAttribute());
        super.save(attribute);
        // 建立商品属性与商品分类的关系
        saveRelation(attribute.getId(), param.getProductCategoryId());
        return true;
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/4/30 15:10
     * @version 1.0
     * @description 根据ID删除商品属性
     */
    @Override
    public boolean removeById(Long id) {
        // 先删除商品属性与商品分类的关系
        deleteRelation(id);
        super.removeById(id);
        return true;
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/4/30 15:10
     * @version 1.0
     * @description 批量删除商品属性
     */
    @Override
    public boolean removeByIds(List<Long> ids) {
        // 先删除商品属性与商品分类的关系
        for (Long id : ids) {
            deleteRelation(id);
        }
        super.removeByIds(ids);
        return true;
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/4/29 14:49
     * @version 1.0
     * @description 更新商品属性，并重新建立商品属性与商品分类的关系
     */
    @Override
    public boolean updateById(ProductAttributeDTO param) {
        ProductAttribute attribute = TransformUtils.copy(param, ProductAttribute.class);
        super.updateById(attribute);
        //先删除原有关系
        deleteRelation(param.getId());
        // 重新建立商品属性与商品分类的关系
        saveRelation(attribute.getId(), param.getProductCategoryId());
        return true;
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/4/30 16:40
     * @version 1.0
     * @description 分页获取商品属性和商品分类名称
     */
    @Override
    public IPage<ProductAttributeVO> page(ProductAttributeQueryDTO param) {
        QueryWrapper<ProductAttribute> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(param.getProductCategoryId())) {
            wrapper.eq("acr.product_category_id", param.getProductCategoryId());
        }
        assemble(TransformUtils.copy(param, ProductAttribute.class), wrapper, param);
        IPage<ProductAttributeVO> objectPageVo = new Page<>(PagingUtil.getPage(), PagingUtil.getLimit());
        return objectPageVo.setRecords(baseMapper.selectPageWithProductCategory(objectPageVo, wrapper));
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/4/29 15:03
     * @version 1.0
     * @description 建立商品属性与商品分类的关系
     */
    private void saveRelation(Long productAttributeId, Long productCategoryId) {
        ProductAttributeCategoryRelation relation = new ProductAttributeCategoryRelation();
        relation.setProductAttributeId(productAttributeId);
        relation.setProductCategoryId(productCategoryId);
        relationMapper.insert(relation);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/4/30 16:18
     * @version 1.0
     * @description 删除商品属性与商品分类的关系
     */
    private void deleteRelation(Long id) {
        LambdaQueryWrapper<ProductAttributeCategoryRelation> wrapper =
                new LambdaQueryWrapper<>(ProductAttributeCategoryRelation.class);
        wrapper.eq(ProductAttributeCategoryRelation::getProductAttributeId, id);
        relationMapper.deleteWithFill(new ProductAttributeCategoryRelation(), wrapper);
    }

    /**
     *功能描述：
     * @author Created by Alex·He on 2020/4/30 16:56
     * @version 1.0
     * @description 模糊查询的参数组合，可通过商品分类名称查询
     */
    private void assemble(ProductAttribute entity, QueryWrapper<ProductAttribute> wrapper, ProductAttributeQueryDTO param) {
        if (null != entity.getId()) {
            wrapper.eq("a.id", entity.getId());
        }
        wrapper.eq("a.delete_status", 1);
        wrapper.eq("acr.delete_status", 1);
        wrapper.eq("c.delete_status", 1);
        if (!StringUtils.isEmpty(param.getProductCategoryName())) {
            wrapper.likeRight("name", param.getProductCategoryName());
        }
        wrapper.orderByDesc("a.create_time");
    }

}
